package com.xbq.oauth.security.sms;


import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * 短信验证码（用户信息）
 * 封装登录信息
 * 身份认证成功之前，存放的是用户手机号
 * 身份认证成功之前，存放的是用户信息
 */
public class SmsCodeAuthenticationToken extends AbstractAuthenticationToken {

    private static final long serialVersionUID = 500L;

    /**
     * 认证前：手机号
     * 认证后：登录成功的用户
     */
    private final Object principal;

    /**
     * 密码，短信验证码中无用
     */
    //private Object credentials;

    public SmsCodeAuthenticationToken(Object principal) {
        super(null);
        this.principal = principal;
        //this.credentials = credentials;
        this.setAuthenticated(false);
    }

    public SmsCodeAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        //this.credentials = credentials;
        super.setAuthenticated(true);
    }

    public Object getCredentials() {
        return null;
    }

    public Object getPrincipal() {
        return this.principal;
    }

    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        } else {
            super.setAuthenticated(false);
        }
    }

    public void eraseCredentials() {
        super.eraseCredentials();
        //this.credentials = null;
    }
}
